//
//  LXDatePickerView.m
//  bgms_p
//
//  Created by lachesis-zhu on 16/6/16.
//  Copyright © 2016年 xiao.zhao@lachesis-mh.com. All rights reserved.
//

#import "LXDatePickerView.h"

@interface LXDatePickerView () <UIPickerViewDataSource, UIPickerViewDelegate>

@property (nonatomic, strong) UIPickerView *timePickerView;

@property (nonatomic, strong) NSCalendar *calendar;

@property (nonatomic, assign) NSInteger todayIndex;

@property (nonatomic, strong) NSMutableArray *yearList;

@property (nonatomic, strong) NSMutableArray *dayList;

@property (nonatomic, assign) NSInteger daysMonth;

@property (nonatomic, assign) NSInteger nowYear;

@property (nonatomic, assign) NSInteger selectMonth;

@property (nonatomic, assign) NSInteger selectYear;

@end

@implementation LXDatePickerView

-(instancetype)initWithFrame:(CGRect)frame
{
    self = [super initWithFrame:frame];
    if(self)
    {
        [self setup];
    }
    
    return self;
}

-(void)setup
{
    _yearList = [[NSMutableArray alloc] init];
    _dayList = [[NSMutableArray alloc] init];
    
    self.calendar = [[NSCalendar alloc] initWithCalendarIdentifier:NSGregorianCalendar];//指定日历的算法
    _calendar.timeZone = [NSTimeZone localTimeZone];
    _calendar.firstWeekday = 1;
    NSDate *dateNow = [NSDate date];
    NSDateComponents *dateComponents = [_calendar components:NSCalendarUnitYear | NSCalendarUnitMonth | NSCalendarUnitDay | NSCalendarUnitHour | NSCalendarUnitMinute fromDate:dateNow];
    _nowYear = dateComponents.year;
    _selectYear = _nowYear;
    _selectMonth = dateComponents.month;
    //这个月有多少天
    NSRange dayRange = [_calendar rangeOfUnit:NSCalendarUnitDay inUnit:NSCalendarUnitMonth forDate:dateNow];
    _daysMonth = dayRange.length;
    
    _timePickerView = [[UIPickerView alloc]initWithFrame:self.bounds];
    _timePickerView.dataSource = self;
    _timePickerView.delegate = self;
    _timePickerView.showsSelectionIndicator = YES;
    [self addSubview:_timePickerView];
    
    [self selectWithComponents:dateComponents];
}

-(void)selectWithComponents:(NSDateComponents *)dateComponents
{
    _todayIndex = dateComponents.day - 1;
    [_timePickerView selectRow:dateComponents.year inComponent:0 animated:YES];
    [_timePickerView selectRow:(dateComponents.month - 1) inComponent:1 animated:YES];
    [_timePickerView selectRow:(dateComponents.day - 1) inComponent:2 animated:YES];
}

-(NSDate *)selectedDate
{
    NSInteger selComponent1 = [_timePickerView selectedRowInComponent:0];
    NSInteger selComponent2 = [_timePickerView selectedRowInComponent:1];
    NSInteger selComponent3 = [_timePickerView selectedRowInComponent:2];
    
    NSDate *dateNow = [NSDate date];
    NSDateComponents *dateComponents = [_calendar components:NSCalendarUnitYear | NSCalendarUnitMonth | NSCalendarUnitDay | NSCalendarUnitHour | NSCalendarUnitMinute fromDate:dateNow];
    
    NSString *dateStr = [NSString stringWithFormat:@"%02ld-%02ld-%02ld %02ld:%02ld", _nowYear - selComponent1, selComponent2 + 1, selComponent3 + 1, dateComponents.hour, dateComponents.minute];
    NSDateFormatter *dateFormatter = [[NSDateFormatter alloc]init];
    dateFormatter.dateFormat = @"yyyy-MM-dd HH:mm";
    
    return [dateFormatter dateFromString:dateStr];
}

#pragma mark -UIPickerViewDataSource
-(NSInteger)numberOfComponentsInPickerView:(UIPickerView *)pickerView
{
    return 3;
}

-(NSInteger)pickerView:(UIPickerView *)pickerView numberOfRowsInComponent:(NSInteger)component
{
    NSInteger rowCount = 0;
    switch (component) {
        case 0:
        {
            rowCount = 6;
        }
            break;
        case 1:
        {
            rowCount = 12;
        }
            break;
        case 2:
        {
            rowCount = _daysMonth;
        }
            break;
        default:
            break;
    }
    
    return rowCount;
}

-(CGFloat)pickerView:(UIPickerView *)pickerView widthForComponent:(NSInteger)component
{
    CGFloat screenWidth = [UIScreen mainScreen].bounds.size.width;
    
    CGFloat width = 0;
    switch (component) {
        case 0:
        {
            width = 120.0f / 375.0f * screenWidth;
        }
            break;
        case 1:
        {
            width = 100.0f / 375.0f * screenWidth;
        }
            break;
        case 2:
        {
            width = 100.0f / 375.0f * screenWidth;
        }
            break;
        default:
            break;
    }
    
    return width;
}

-(CGFloat)pickerView:(UIPickerView *)pickerView rowHeightForComponent:(NSInteger)component
{
    return 44.0f;
}

-(NSAttributedString *)pickerView:(UIPickerView *)pickerView attributedTitleForRow:(NSInteger)row forComponent:(NSInteger)component
{
    NSString *title = nil;
    switch (component) {
        case 0:
        {
            title = [NSString stringWithFormat:@"%02ld", _nowYear - row];
        }
            break;
        case 1:
        {
            title = [NSString stringWithFormat:@"%02ld", row + 1];
        }
            break;
        case 2:
        {
            title = [NSString stringWithFormat:@"%02ld", row + 1];
        }
            break;
        default:
            break;
    }
    
    NSAttributedString *attrStr = [[NSAttributedString alloc] initWithString:title attributes:@{NSForegroundColorAttributeName : kAppThemeColor, NSFontAttributeName : [UIFont lx_systemFontOfSize:18.0f]}];
    
    return attrStr;
}

-(void)pickerView:(UIPickerView *)pickerView didSelectRow:(NSInteger)row inComponent:(NSInteger)component
{
    if (component != 2) {
        NSCalendar *calendar = [[NSCalendar alloc] initWithCalendarIdentifier:NSGregorianCalendar];
        calendar.timeZone = [NSTimeZone localTimeZone];
        calendar.firstWeekday = 1;
        NSDateFormatter *formatter = [[NSDateFormatter alloc] init];
        [formatter setDateFormat:@"yyyy-MM-dd"];
        if (component == 1) {
            _selectMonth = row + 1;
            NSDate *newDate = [formatter dateFromString:[NSString stringWithFormat:@"%ld-%2ld-02", _selectYear, _selectMonth]];
            
            //这个月有多少天
            NSRange dayRange = [calendar rangeOfUnit:NSCalendarUnitDay inUnit:NSCalendarUnitMonth forDate:newDate];
            _daysMonth = dayRange.length;
        } else if (component == 0) {
            _selectYear = _nowYear - row;
            NSDate *newDate = [formatter dateFromString:[NSString stringWithFormat:@"%ld-%2ld-02", _selectYear, _selectMonth]];
            //这个月有多少天
            NSRange dayRange = [calendar rangeOfUnit:NSDayCalendarUnit inUnit:NSMonthCalendarUnit forDate:newDate];
            _daysMonth = dayRange.length;
        }
        [pickerView reloadAllComponents];
        if (_todayIndex < _daysMonth) {
            [pickerView selectRow:_todayIndex inComponent:2 animated:YES];
        } else {
            [pickerView selectRow:0 inComponent:2 animated:YES];
        }
    }
}
@end
